package com.zut.hospital.service.pharmacy;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.pharmacy.InventoryMapper;
import com.zut.hospital.dao.pharmacy.MedicineMapper;
import com.zut.hospital.dao.pharmacy.SupplierMapper;
import com.zut.hospital.entity.pharmacy.*;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-06-07 18:18
 **/
@Service

public class MedicineServiceImpl implements MedicineService {
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    InventoryMapper inventoryMapper;

    public String layuiJsonParse(List<Map<String, String>> list,int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }
    public String layuiJsonParsefail(List<Map<String, String>> list,int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "failed");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    @Override
    public String findMedByMedNameConsole(String med_name){
        Map<String,String>map;
        List<Medicine> medicineList=medicineMapper.selectMoHoName(med_name);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int a=0;
        for (Medicine medicine:medicineList){
            map=new HashMap<>();

            map.put("name",medicine.getMdcName());
            resaultmap.add(map);
            a++;
        }
        System.out.println(resaultmap.toString());
         if(a>0) {
             Map<String, Object> layuiMap = new HashMap<>();
             layuiMap.put("code", 1);
             layuiMap.put("msg", "success");
             layuiMap.put("count", a);
             layuiMap.put("data", resaultmap);
             return JSON.toJSONString(layuiMap);
         }
         else
             return layuiJsonParsefail(resaultmap,a);

}
/*
* public String pagePurchase(int currPage,int pageSize){
        int size=getAll().size();
        PurchaseExample purchaseExample=new PurchaseExample();
        List<Purchase>purchaseList= purchaseMapper.selectByExample(purchaseExample);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int firstIndex=(currPage-1)*pageSize;
        int lastIndex=currPage*pageSize;
        if(currPage*pageSize>size){
             lastIndex=size;
        }
        for (Purchase purchase:purchaseList.subList(firstIndex,lastIndex)){
            resaultmap.add(selectAll(purchase));
        }
        return layuiJsonParse(resaultmap,purchaseList.size());
    }*/
public Map selectAll(Medicine medicine){
    Map<String,String>map;

    InventoryExample inventoryExample=new InventoryExample();
    inventoryExample.createCriteria().andIvtrMdcIdEqualTo(medicine.getId());
    Inventory inventory=inventoryMapper.selectByExample(inventoryExample).get(0);
    map=new HashMap<>();
    map.put("name",medicine.getMdcName());
    map.put("id",medicine.getId().toString());
    map.put("unit",medicine.getMdcUnit());
    map.put("company",medicine.getMdcCompany());
    map.put("jinjia",medicine.getMdcRemark());
    map.put("inv",inventory.getIvtrCount().toString());
    map.put("limit",inventory.getIvtrLimit().toString());
    return map;
}
    //分页药品
    @Override
    public String pageallMed(int currPage,int pageSize){
        Map<String,String>map;
        MedicineExample medicineExample=new MedicineExample();
        List<Medicine> medicineList=medicineMapper.selectByExample(medicineExample);
        int size=medicineList.size();
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int firstIndex=(currPage-1)*pageSize;
        int lastIndex=currPage*pageSize;
        if(currPage*pageSize>size){
            lastIndex=size;
        }
        for (Medicine medicine:medicineList.subList(firstIndex,lastIndex)){
            resaultmap.add(selectAll(medicine));
        }
        return layuiJsonParse(resaultmap,size);

    }

    @Override
    public int insertMed(String mdc_name,String mdc_unit,String mdc_spec,String mdc_company,String mdc_remark){
     MedicineExample medicineExample=new MedicineExample();
     Medicine medicine=new Medicine();
     medicine.setMdcName(mdc_name);
     medicine.setMdcRemark(mdc_remark);
     medicine.setMdcCompany(mdc_company);
     medicine.setMdcUnit(mdc_unit);
     medicine.setMdcSpec(mdc_spec);

     medicineMapper.insert(medicine);
     medicineExample.createCriteria().andMdcNameEqualTo(mdc_name);
     Medicine medicine1=medicineMapper.selectByExample(medicineExample).get(0);
        Inventory inventory=new Inventory();
        inventory.setIvtrMdcId(medicine1.getId());
        inventory.setIvtrCount(0);
        inventory.setIvtrLimit(10);
        inventory.setIvtrRemark("");
        inventoryMapper.insert(inventory);
     return  1;
    }

    @Override
    public String updateMed(int id,String field,String value){
        int count;
        double price;
        MedicineExample medicineExample=new MedicineExample();
        medicineExample.createCriteria().andIdEqualTo(id);
        Medicine medicine=medicineMapper.selectByExample(medicineExample).get(0);

        InventoryExample inventoryExample=new InventoryExample();
        inventoryExample.createCriteria().andIvtrMdcIdEqualTo(medicine.getId());
        Inventory inventory=inventoryMapper.selectByExample(inventoryExample).get(0);
        if (field.equals("name")){
            medicine.setMdcName(value);
        }
        else if ((field.equals("unit"))){
            medicine.setMdcUnit(value);
        }
        else if ((field.equals("company"))){
            medicine.setMdcCompany(value);
        }
        else if ((field.equals("jinjia"))){
            medicine.setMdcRemark(value);
        }
        else if ((field.equals("limit"))){

            inventory.setIvtrLimit(Integer.parseInt(value));
        }
        medicineMapper.updateByExample(medicine,medicineExample);
        inventoryMapper.updateByExample(inventory,inventoryExample);

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", "");
        return JSON.toJSONString(layuiMap);
    }

}
